var callbackFn;
layui.use(['form', 'layer', 'laydate', 'util'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laydate = layui.laydate,
        util = layui.util,
        $ = layui.jquery;

    laydate.render({
        elem: '#createTime'
        , type: 'datetime'
    });
    laydate.render({
        elem: '#firstSearchTime'
        , type: 'datetime'
    });
    laydate.render({
        elem: '#validateTime'
        , type: 'datetime'
    });


    form.on("submit(add)", function (data) {
        var jsondata = {
            id: $("#id").val(),
            name: $("#name").val(),
            code: $("#code").val(),
            createTime: $("#createTime").val(),
            firstSearchTime: $("#firstSearchTime").val(),
            searchNum: $("#searchNum").val(),
            validateTime: $("#validateTime").val(),
        };
        $.ajax({
            type: 'post',
            url: "/antiFakeSys/antiFakeCode/save.do",
            async: false,
            data: JSON.stringify(jsondata),
            success: function (res) {
                if (res.code == 0) {
                    top.layer.msg("操作成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                    form.render();
                } else {
                    layer.msg(res.msg);
                }
            },
            contentType: 'application/json;charset=utf-8',
            dataType: 'json'
        });
    });

    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    function loadData() {
        var id = getQueryString("id");
        if (id) {
            $.get("/antiFakeSys/antiFakeCode/get/" + id + ".do", function (res) {
                var antiFakeCode = res.data;
                $("#id").val(antiFakeCode.id);
                $("#name").val(antiFakeCode.name);
                $("#code").val(antiFakeCode.code);
                $("#createTime").val(util.toDateString(antiFakeCode.createTime, 'yyyy-MM-dd HH:mm:ss'));
                $("#firstSearchTime").val(util.toDateString(antiFakeCode.firstSearchTime, 'yyyy-MM-dd HH:mm:ss'));
                $("#searchNum").val(antiFakeCode.searchNum);
                $("#validateTime").val(util.toDateString(antiFakeCode.validateTime, 'yyyy-MM-dd HH:mm:ss'));
                form.render();
            })
        }
    }

    loadData();     //加载数据

    function layer_show_parent(url, title) {
        if (title == null || title == '') {
            title = false;
        }
        var page = layui.layer.open({
            id: "test-fet",
            type: 2,
            area: ['90%', '90%'],
            fix: false,
            maxmin: true,
            shade: 0.4,
            title: title,
            content: url
        });
        return page;
    }
});